
import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SetUrlPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SetUrlPageState();
  }
}

class SetUrlPageState extends State<SetUrlPage>{


  /// http://122.51.164.176:8072
  var urltf = TextEditingController();


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    syncInit();

  }

  void syncInit()async{

    var prefs = await SharedPreferences.getInstance();

    var urlStr = await PersistentStorage().hasKey('urlStr');

    if(urlStr == true){
      urltf.text = prefs.getString('urlStr');
    }else{
      // urltf.text = 'http://202.136.212.84:8069';
      // urltf.text = 'http://122.51.164.176:8072';
      urltf.text = 'http://36.153.159.20:8072';
      // prefs.setString('urlStr', '${urltf.text}');
    }

    // prefs.setString('urlStr', 'http://122.51.164.176:8072');
    //
    // urltf.text = prefs.getString('urlStr');


  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('设置URL'),
        actions: [
          TextButton(onPressed: ()async{

            if(urltf.text == 'http://'){
              // Toast.toast(context,msg: '请输入正确的url');
              BotToast.showText(text: '请输入正确的url');
              return;
            }
            var prefs = await SharedPreferences.getInstance();
            prefs.setString('urlStr', urltf.text);
            setState(() {

            });
            Get.back();
          }, child: Text('保存',style: TextStyle(color: Colors.white),)),
        ],
      ),
      body: Column(
        children: [
         Center(child: Container(

           margin: EdgeInsets.only(top: 20),
           padding: EdgeInsets.only(left: 10),
           decoration: new BoxDecoration(
//背景
             color: Theme.of(context).cardColor,
             //设置四周圆角 角度
             borderRadius: BorderRadius.all(Radius.circular(4.0)),
             //设置四周边框
             border: new Border.all(width: 1, color: Colors.grey[200]),


           ),
           width: Get.width,
           child:  TextField(
             onChanged: (v){

             },
             decoration: InputDecoration(

               border: InputBorder.none

             ),
             controller: urltf,
             keyboardType: TextInputType.url,
             // textInputAction: TextInputAction.none,
           ),

         ),)
        ],
      ),
    );
  }
}